/**
 * 
 */
package reco.db.validation;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.util.regex.Pattern;

import reco.db.Constraint;

/**
 * Restriccion empleada para asegurarse que un campo es una direccion IpConstraint.
 */
public final class IpConstraint extends Constraint<String> {

    private static final String OCTET = "((\\d)|([01]?\\d\\d)|(2[0-4]\\d)|(25[0-5]))";
    private static final String DOT = "\\.";
    private static final Pattern IP_PATTERN = Pattern.compile("^" + OCTET + DOT + OCTET + DOT + OCTET + DOT + OCTET + "$");

    public IpConstraint() {
        super("ip", "noIp");
    }

    @Override
    public boolean validate(String value) {
        if (isBlank(value)) {
            return true;
        }
        return IP_PATTERN.matcher(value).matches();
    }

    @Override
    public Object[] getErrorArguments() {
        return null;
    }

}
